package com.uniledger.exchange.home.scheme;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.uniledger.commonlib.easymoney.EasyMoneyTextView;
import com.uniledger.commonlib.easymoney.MoneyTextView;
import com.uniledger.exchange.R;
import com.uniledger.exchange.model.StellarAsset;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import es.dmoral.prefs.Prefs;

public class HomeAssetAdapter extends BaseAdapter {

    private Context context;
    private List<StellarAsset> list;
    private LayoutInflater inflater;

    private boolean isOpen;

    public HomeAssetAdapter(Context context) {
        this.context = context;
        this.list = new ArrayList<>();
        this.inflater = LayoutInflater.from(context);
        this.isOpen = Prefs.with(context).readBoolean("HomeAsset", true);
    }

    public void addData(List<StellarAsset> assets) {
        list.addAll(assets);
        notifyDataSetChanged();
    }

    public void toggle(boolean isOpen){
        this.isOpen=isOpen;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public StellarAsset getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        VH vh;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_home_asset, parent, false);
            vh = new VH(convertView);
            convertView.setTag(vh);
        } else {
            vh = (VH) convertView.getTag();
        }

        StellarAsset asset = getItem(position);

        vh.assertType.setText(asset.currency);

        if (isOpen){
            vh.assertBalance.setText(asset.getAmount());
        }else {
            vh.assertBalance.setText("****");
        }
        return convertView;
    }

    static class VH {
        @BindView(R.id.txt_asset_type)
        TextView assertType;
        @BindView(R.id.txt_asset_balance)
        TextView assertBalance;

        public VH(View view) {
            ButterKnife.bind(this, view);
        }

    }
}
